package com.atguigu.daijia.customer.client.fallback;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @create: 2024/9/21
 * @author: gyhe10
 * @description: CustomerInfoFeignClient 接口的降级实现类
 **/

@Slf4j
@Component
public class CustomerInfoFeignClientFallbackFactory implements FallbackFactory<CustomerInfoFeignClient> {
    @Override
    public CustomerInfoFeignClient create(Throwable cause) {
        return new CustomerInfoFeignClient() {
            @Override
            public Result<Long> login(String code) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<CustomerLoginVo> getCustomerLoginInfo(Long customerId) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<Boolean> updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<String> getCustomerOpenId(Long customerId) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }
        };
    }
}
